#!/usr/bin/env python3
#coding=utf8

import re
###################### 公用 ####################################
PORTS = [
    '21',     #ftp
    '22',     #ssh
    '23',     #telnet
    '80',     #http
    '443',    #https
    '1433',   #mssql
    '3306',   #mysql
    '3389',   #mstsc
    '1521',   #oracle
    '5432',   #postgresql
    '6379',   #redis
    '8080',   #tomcat
    '11211',  #memcache
    '27017',  #mongodb
]
######################## WEB用 ######################################
def vmobile(mobile):
    return re.match(r"((13|14|15|18)\d{9}$)|(\w+[@]\w+[.]\w+)", mobile)
    
######################## service用 #################################
def ip2num(ip):
    ip = [int(x) for x in ip.split('.')]
    return ip[0]<<24|ip[1]<<16|ip[2]<<8|ip[3]

def num2ip(num):
    return '%s.%s.%s.%s'%((num&0xff000000)>>24,(num&0x00ff0000)>>16,(num&0x0000ff00)>>8,(num&0x000000ff))

def get_hosts(text):
    text = text.strip()
    if '-' in text:
        start,end = [ip2num(x) for x in text.split('-')]
    elif '/' in text:
        ips=text.split('/')
        start = ip2num(ips[0])
        end = ips[0].split('.')
        end[3] = ips[1]
        end = ip2num('.'.join(end))
    else:
        return [text]
    return [num2ip(num) for num in range(start, end + 1) if num & 0xff]

def get_ports(text):
    text = text.strip()
    if '-' in text:
        ports=text.split('-')
        return [x for x in range(int(ports[0]),int(ports[1])+1)]
    else:
        return [int(x) for x in text.split(',')]

def test_private_ip(ip):
    ret = ip.split('.')
    if not len(ret) == 4:
        return True
    if ret[0] == '10':
        return True
    if ret[0] == '172' and 16 <= int(ret[1]) <= 32:
        return True
    if ret[0] == '192' and ret[1] == '168':
        return True
    return False